//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;

namespace CommunityServer.Controls
{
	/// <summary>
	/// Summary description for TagPostResults.
	/// </summary>
	public class TagPostResults : TemplatedWebControl 
	{
		protected CSContext csContext = CSContext.Current;
		protected Repeater resultsRepeater;
		protected CurrentPage resultsTotal;
		protected Literal resultsDuration;
		protected Pager pager;
		protected StatusMessage status;
		protected PlaceHolder resultsContainer;

		#region Properties

		public string Tags
		{
			get
			{
				return String.Join(",", FilterTags);
			}
			set
			{
				ArrayList tags = new ArrayList();
				foreach (string tag in value.Split(','))
				{
					if (!Globals.IsNullorEmpty(tag.Trim()))
						tags.Add(tag.Trim());
				}
				FilterTags = (string[]) tags.ToArray(typeof(string));
			}
		}

		bool _logicallyOrTags = false;
		public bool LogicallyOrTags
		{
			get
			{
				return _logicallyOrTags;
			}
			set
			{
				_logicallyOrTags = value;
			}
		}

		string[] _filterTags = null;
		public string[] FilterTags
		{
			get
			{
				if (_filterTags == null)
					return csContext.Tags;
				else
					return _filterTags;
			}
			set
			{
				_filterTags = value;
			}
		}

		ApplicationType _applicationType = ApplicationType.Unknown;
		public ApplicationType ApplicationType
		{
			get
			{
				return _applicationType;
			}
			set
			{
				_applicationType = value;
			}
		}

		int _sectionID = -1;
		public int SectionID
		{
			get
			{
				return _sectionID;
			}
			set
			{
				_sectionID = value;
			}
		}

		SearchResultSet _searchResults = null;
		protected virtual SearchResultSet SearchResults
		{
			get
			{
				if (_searchResults == null && this.FilterTags != null)
				{
					EnsureChildControls();

					if (this.SectionID > 0)
					{
						if (pager != null)
							_searchResults = CommunityServer.Components.Tags.GetPostsMatchingTags(this.SectionID, this.FilterTags, pager.PageIndex, pager.PageSize, !this.LogicallyOrTags);
						else
							_searchResults = CommunityServer.Components.Tags.GetPostsMatchingTags(this.SectionID, this.FilterTags, 0, csContext.SiteSettings.SearchPostsPerPage);
					}
					else if (this.ApplicationType != ApplicationType.Unknown)
					{
						if (pager != null)
							_searchResults = CommunityServer.Components.Tags.GetPostsMatchingTags(this.ApplicationType, this.FilterTags, pager.PageIndex, pager.PageSize, !this.LogicallyOrTags);
						else
							_searchResults = CommunityServer.Components.Tags.GetPostsMatchingTags(this.ApplicationType, this.FilterTags, 0, csContext.SiteSettings.SearchPostsPerPage);
					}
					else
					{
						if (pager != null)
							_searchResults = CommunityServer.Components.Tags.GetPostsMatchingTags(this.FilterTags, pager.PageIndex, pager.PageSize, !this.LogicallyOrTags);
						else
							_searchResults = CommunityServer.Components.Tags.GetPostsMatchingTags(this.FilterTags, 0, csContext.SiteSettings.SearchPostsPerPage);
					}
				}
				
				return _searchResults;
			}
		}

		#endregion

		protected override void OnLoad(EventArgs e) 
		{
			base.OnLoad (e);

			this.DataBind();
		}

		protected override void AttachChildControls() 
		{
			resultsRepeater = FindControl("ResultsRepeater") as Repeater;
			pager = (Pager) FindControl("Pager");
			resultsTotal = (CurrentPage) FindControl("ResultsTotal");
			resultsDuration = (Literal) FindControl("ResultsDuration");
			status = FindControl("Status") as StatusMessage;
			resultsContainer = FindControl("ResultsContainer") as PlaceHolder;

			resultsRepeater.ItemDataBound += new RepeaterItemEventHandler( ResultsRepeater_ItemDataBound );

			if (resultsDuration != null)
				resultsDuration.Text = ResourceManager.GetString("SearchResults_SearchDuration");

			if (resultsTotal != null)
				resultsTotal.TextFormat = ResourceManager.GetString("SearchResults_TotalResults");

			if (pager != null)
				pager.PageSize = csContext.SiteSettings.SearchPostsPerPage;
		}

		public override void DataBind() 
		{
			base.DataBind();

			if (this.SearchResults == null)
			{
				if (resultsContainer != null)
					resultsContainer.Visible = false;

				if (status != null)
					status.Visible = false;

				return;
			}
			else if (this.SearchResults.Posts.Count == 0)
			{
				if (status != null)
				{
					status.Visible = true;
					status.Success = false;
					status.ResourceName = "SearchView_NoResults";
				}

				return;
			}

			resultsRepeater.DataSource = this.SearchResults.Posts;
			resultsRepeater.DataBind();

			if (pager != null)
			{
				int resultCountUpper, resultCountLower;

				pager.TotalRecords = this.SearchResults.TotalRecords;
				resultCountLower = 1 + (pager.PageIndex * pager.PageSize);

				if (pager.TotalRecords > pager.PageSize) 
					resultCountUpper = pager.PageSize + (pager.PageSize * pager.PageIndex);
				else
					resultCountUpper = this.SearchResults.TotalRecords;

				if (resultsTotal != null)
				{
					resultsTotal.PageIndex = pager.PageIndex;
					resultsTotal.TotalPages = pager.CalculateTotalPages();
					resultsTotal.TotalRecords = pager.TotalRecords;
				}
			}
			else if (resultsTotal != null)
				resultsTotal.Visible = false;

			if (resultsDuration != null)
			{
				if (this.SearchResults.SearchDuration < 1)
					resultsDuration.Text = string.Format(resultsDuration.Text, "< 1");
				else
					resultsDuration.Text = string.Format(resultsDuration.Text, this.SearchResults.SearchDuration);
			}
		}

		protected virtual void ResultsRepeater_ItemDataBound (Object Sender, RepeaterItemEventArgs e) 
		{
			IndexPost post = e.Item.DataItem as IndexPost;
			if (post == null)
				return;
            
			switch (e.Item.ItemType) 
			{
				case  ListItemType.Item:
				case  ListItemType.AlternatingItem:
				
					HyperLink SectionUrl = e.Item.FindControl("SectionUrl") as HyperLink;
					if(SectionUrl != null && Globals.IsNullorEmpty(SectionUrl.NavigateUrl))
					{
						CSApplicationData data = ApplicationSet.Data(post.ApplicationType);
						if(data != null)
						{
							SectionUrl.NavigateUrl = data.SectionUrl(post);
						}
						else
						{
							SectionUrl.Visible = false;
						}
					}

					break;
			}
		}
	}
}
